<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Algorithm," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="题目来源北航某次算法测试题。
问题描述将由数字和四则运算符组成的后缀表达式变换为中缀表达式，输入的后缀表达式的运算符不超过15个，要求转换后的中缀表达式中不出现不必要的括号。例如，整个表达式的括号要省略，不影响计算顺序括号要省略。
输入形式程序从标准输入读入一行字符串，是一个合法的后缀表达式，数字和运算符之间由空格分隔。数字可以是带小数部分的浮点数。
输出形式向标准输出打印结果，输出只有一行，是转">
<meta property="og:type" content="article">
<meta property="og:title" content="后缀表达式转中缀表达式">
<meta property="og:url" content="http://www.vitah.net/posts/c02ba867/index.html">
<meta property="og:site_name" content="Vitah's Blog">
<meta property="og:description" content="题目来源北航某次算法测试题。
问题描述将由数字和四则运算符组成的后缀表达式变换为中缀表达式，输入的后缀表达式的运算符不超过15个，要求转换后的中缀表达式中不出现不必要的括号。例如，整个表达式的括号要省略，不影响计算顺序括号要省略。
输入形式程序从标准输入读入一行字符串，是一个合法的后缀表达式，数字和运算符之间由空格分隔。数字可以是带小数部分的浮点数。
输出形式向标准输出打印结果，输出只有一行，是转">
<meta property="og:updated_time" content="2016-12-28T15:26:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="后缀表达式转中缀表达式">
<meta name="twitter:description" content="题目来源北航某次算法测试题。
问题描述将由数字和四则运算符组成的后缀表达式变换为中缀表达式，输入的后缀表达式的运算符不超过15个，要求转换后的中缀表达式中不出现不必要的括号。例如，整个表达式的括号要省略，不影响计算顺序括号要省略。
输入形式程序从标准输入读入一行字符串，是一个合法的后缀表达式，数字和运算符之间由空格分隔。数字可以是带小数部分的浮点数。
输出形式向标准输出打印结果，输出只有一行，是转">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.vitah.net/posts/c02ba867/"/>





  <title> 后缀表达式转中缀表达式 | Vitah's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Vitah's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://www.vitah.net/posts/c02ba867/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Vitah">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/head.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Vitah's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                后缀表达式转中缀表达式
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-12-28T23:26:33+08:00">
                2016-12-28
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Algorithm/" itemprop="url" rel="index">
                    <span itemprop="name">Algorithm</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/posts/c02ba867/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="posts/c02ba867/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="题目来源"><a href="#题目来源" class="headerlink" title="题目来源"></a>题目来源</h3><p>北航某次算法测试题。</p>
<h3 id="问题描述"><a href="#问题描述" class="headerlink" title="问题描述"></a>问题描述</h3><p>将由数字和四则运算符组成的后缀表达式变换为中缀表达式，输入的后缀表达式的运算符不超过15个，要求转换后的中缀表达式中不出现不必要的括号。例如，整个表达式的括号要省略，不影响计算顺序括号要省略。</p>
<h4 id="输入形式"><a href="#输入形式" class="headerlink" title="输入形式"></a>输入形式</h4><p>程序从标准输入读入一行字符串，是一个合法的后缀表达式，数字和运算符之间由空格分隔。数字可以是带小数部分的浮点数。</p>
<h4 id="输出形式"><a href="#输出形式" class="headerlink" title="输出形式"></a>输出形式</h4><p>向标准输出打印结果，输出只有一行，是转换后的中缀表达式。并且：</p>
<ul>
<li>各分量（包括括号）紧密输出，不使用空格进行分隔；</li>
<li>在转换后的各运算数的出现顺序不变；</li>
<li>浮点数保留输入时的小数位数。</li>
</ul>
<a id="more"></a>
<h3 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt; stdio.h &gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt; stdlib.h &gt;</span></span></div><div class="line"></div><div class="line"><span class="keyword">struct</span> Expression;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> Expression * Node;</div><div class="line"></div><div class="line"><span class="keyword">struct</span> Expressionlist;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> Expressionlist * Elist;</div><div class="line"></div><div class="line"><span class="comment">//结点，包含一个符号信息和next指针</span></div><div class="line"><span class="keyword">struct</span> Expression &#123;</div><div class="line">    <span class="keyword">char</span> c;</div><div class="line">    Node next;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">//表达式指针数组</span></div><div class="line"><span class="keyword">struct</span> Expressionlist &#123;</div><div class="line">    <span class="keyword">int</span> size;</div><div class="line">    Node * Lists;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">//定义全局变量</span></div><div class="line"><span class="keyword">int</span> node_position = <span class="number">0</span>;</div><div class="line"></div><div class="line"><span class="comment">//初始化，注意此处函数返回的是指针</span></div><div class="line"><span class="function">Elist <span class="title">InitiaExpressionList</span><span class="params">(<span class="keyword">int</span> s)</span> </span>&#123;</div><div class="line">    <span class="comment">//声明一个表达式指针数组</span></div><div class="line">    Elist E;</div><div class="line">    <span class="keyword">int</span> i;</div><div class="line"></div><div class="line">    <span class="comment">//开辟空间存储size个结点,分配Lists数组的空间</span></div><div class="line">    E = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expressionlist));</div><div class="line">    E - &gt; size = s;</div><div class="line">    E - &gt; Lists = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(Node) * E - &gt; size);</div><div class="line"></div><div class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; E - &gt; size; i++) &#123;</div><div class="line">        <span class="comment">//分配空间，然后把Lists数组的指针指向这个空间</span></div><div class="line">        E - &gt; Lists[i] = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expression));</div><div class="line">        E - &gt; Lists[i] - &gt; next = <span class="literal">NULL</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">return</span> E;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//在结点数组中，找到为空的结点</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">FindEmptyNode</span><span class="params">(Elist E)</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> i;</div><div class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; E - &gt; size; i++)</div><div class="line">        <span class="keyword">if</span> (E - &gt; Lists[i] - &gt; next == <span class="literal">NULL</span>)</div><div class="line">            <span class="keyword">return</span> i;</div><div class="line"></div><div class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//在指针数组的指定位置的末尾添加结点信息</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">CreateNode</span><span class="params">(Elist E, <span class="keyword">int</span> flag, <span class="keyword">char</span> ch)</span> </span>&#123;</div><div class="line">    Node newNode, p;</div><div class="line"></div><div class="line">    p = E - &gt; Lists[flag];</div><div class="line"></div><div class="line">    <span class="comment">//当该链表中只有一个数时把该链表的运算符定义为*(或/)</span></div><div class="line">    p - &gt; c = <span class="string">'*'</span>;</div><div class="line"></div><div class="line">    <span class="keyword">while</span> (p - &gt; next != <span class="literal">NULL</span>)</div><div class="line">        p = p - &gt; next;</div><div class="line"></div><div class="line">    newNode = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expression));</div><div class="line">    newNode - &gt; c = ch;</div><div class="line">    newNode - &gt; next = p - &gt; next;</div><div class="line">    p - &gt; next = newNode;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//重置结点为空</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">EmptyNode</span><span class="params">(Elist E, <span class="keyword">int</span> flag)</span> </span>&#123;</div><div class="line">    Node p, r;</div><div class="line">    E - &gt; Lists[flag] - &gt; c = <span class="string">'+'</span>;</div><div class="line">    p = E - &gt; Lists[flag] - &gt; next;</div><div class="line"></div><div class="line">    <span class="keyword">while</span> (p != <span class="literal">NULL</span>) &#123;</div><div class="line">        r = p;</div><div class="line">        p = p - &gt; next; <span class="comment">//r作为p的前一个结点</span></div><div class="line">        E - &gt; Lists[flag] - &gt; next = p;</div><div class="line">        <span class="built_in">free</span>(r);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">free</span>(p);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//输出链表信息</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">PrintNode</span><span class="params">(Node n)</span> </span>&#123;</div><div class="line">    Node p; <span class="comment">//定义指针表示输出的结点</span></div><div class="line">    p = n - &gt; next; <span class="comment">//若代码是p=L; 会输出L里面的废信息，p=L-&gt;next跳过头指针的废信息</span></div><div class="line"></div><div class="line">    <span class="keyword">if</span> (p == <span class="literal">NULL</span>) <span class="comment">//若p一开始就为空，即L-&gt;next = NULL，此时链表为空</span></div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"空\n"</span>);</div><div class="line">        <span class="built_in">exit</span>(<span class="number">0</span>);</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="keyword">while</span> (<span class="number">1</span>) <span class="comment">//一直循环输出，直到p为空</span></div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span> (p == <span class="literal">NULL</span>)</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%c"</span>, p - &gt; c);</div><div class="line">            p = p - &gt; next;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//连接两个链表</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ConnectNode</span><span class="params">(Elist E, <span class="keyword">int</span> flag, <span class="keyword">char</span> ch)</span> </span>&#123;</div><div class="line">    Node p, q, f;</div><div class="line"></div><div class="line">    <span class="keyword">if</span> (flag == <span class="number">0</span>) &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"错误\n"</span>);</div><div class="line">        <span class="built_in">exit</span>(<span class="number">0</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    E - &gt; Lists[flag - <span class="number">1</span>] - &gt; c = ch;</div><div class="line"></div><div class="line">    p = E - &gt; Lists[flag - <span class="number">1</span>];</div><div class="line">    f = E - &gt; Lists[flag] - &gt; next;</div><div class="line"></div><div class="line">    <span class="keyword">while</span> (p - &gt; next != <span class="literal">NULL</span>)</div><div class="line">        p = p - &gt; next;</div><div class="line"></div><div class="line">    q = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expression));</div><div class="line">    q - &gt; c = ch;</div><div class="line">    q - &gt; next = p - &gt; next;</div><div class="line">    p - &gt; next = q;</div><div class="line">    p = q;</div><div class="line"></div><div class="line">    <span class="comment">//此处连接两个链表应该把第二个链表内全部结点的信息复制到第一个链表中，而不是仅仅是把第一个</span></div><div class="line">    <span class="comment">//链表的尾结点指向第二个链表的头结点，因为第二个链表会执行清空操作</span></div><div class="line">    <span class="keyword">while</span> (f != <span class="literal">NULL</span>) &#123;</div><div class="line">        q = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expression));</div><div class="line">        q - &gt; c = f - &gt; c;</div><div class="line">        q - &gt; next = p - &gt; next;</div><div class="line">        p - &gt; next = q;</div><div class="line">        p = q;</div><div class="line">        f = f - &gt; next;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">//第二个链表执行清空操作</span></div><div class="line">    EmptyNode(E, flag);</div><div class="line">    node_position--;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//在结点数组的指定位置的首尾添加括号</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">BracketNode</span><span class="params">(Elist E, <span class="keyword">int</span> flag)</span> </span>&#123;</div><div class="line">    Node begin, end, p; <span class="comment">//p作为标记结点，标志链表尾结点</span></div><div class="line"></div><div class="line">    begin = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expression)); <span class="comment">//在链表头部插入左括号符'('</span></div><div class="line">    begin - &gt; c = <span class="string">'('</span>;</div><div class="line">    begin - &gt; next = E - &gt; Lists[flag] - &gt; next;</div><div class="line">    E - &gt; Lists[flag] - &gt; next = begin;</div><div class="line"></div><div class="line">    p = E - &gt; Lists[flag];</div><div class="line">    <span class="keyword">while</span> (p - &gt; next != <span class="literal">NULL</span>)</div><div class="line">        p = p - &gt; next;</div><div class="line"></div><div class="line">    end = <span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Expression)); <span class="comment">//在链表尾部插入右括号符')'</span></div><div class="line">    end - &gt; c = <span class="string">')'</span>;</div><div class="line">    p - &gt; next = end;</div><div class="line">    end - &gt; next = <span class="literal">NULL</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    Elist E;</div><div class="line">    <span class="keyword">int</span> i;</div><div class="line">    <span class="keyword">char</span> str[<span class="number">80</span>]; <span class="comment">//存储输入的后缀表达式</span></div><div class="line">    <span class="keyword">int</span> space_flag = <span class="number">0</span>; <span class="comment">//当前字符是否是空格的标记</span></div><div class="line"></div><div class="line">    E = InitiaExpressionList(<span class="number">10</span>); <span class="comment">//初始化</span></div><div class="line">    gets(str); <span class="comment">//从键盘读入后缀表达式的字符串</span></div><div class="line"></div><div class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; str[i] != <span class="string">'\0'</span>; i++) &#123;</div><div class="line">        <span class="keyword">if</span> ((str[i] &gt;= <span class="string">'0'</span> &amp;&amp; str[i] &lt;= <span class="string">'9'</span>) || str[i] == <span class="string">'.'</span>) &#123;</div><div class="line">            <span class="keyword">if</span> (space_flag == <span class="number">1</span>) <span class="comment">//上一个字符是空白字符，将读入的字符存入新链表</span></div><div class="line">            &#123;</div><div class="line">                node_position++;</div><div class="line">                CreateNode(E, node_position, str[i]);</div><div class="line">            &#125; <span class="keyword">else</span> <span class="comment">//否则存入当前链表</span></div><div class="line">                CreateNode(E, node_position, str[i]);</div><div class="line"></div><div class="line">            space_flag = <span class="number">0</span>;</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (str[i] == <span class="string">' '</span>) <span class="comment">//当前字符是空白字符跳过本次循环</span></div><div class="line">        &#123;</div><div class="line">            space_flag = <span class="number">1</span>;</div><div class="line">            <span class="keyword">continue</span>;</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (str[i] == <span class="string">'+'</span> || str[i] == <span class="string">'-'</span>) <span class="comment">//读入+或-运算符把两个链表连接成一个</span></div><div class="line">        &#123;</div><div class="line">            space_flag = <span class="number">0</span>;</div><div class="line">            ConnectNode(E, node_position, str[i]);</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (str[i] == <span class="string">'*'</span> || str[i] == <span class="string">'/'</span>) <span class="comment">//读入* /运算符</span></div><div class="line">        &#123;</div><div class="line">            space_flag = <span class="number">0</span>;</div><div class="line"></div><div class="line">            <span class="comment">//判断两个链表的符号，如果优先级低的话在该链表两端加上括号</span></div><div class="line">            <span class="keyword">if</span> (E - &gt; Lists[node_position] - &gt; c == <span class="string">'+'</span> || E - &gt; Lists[node_position] - &gt; c == <span class="string">'-'</span>)</div><div class="line">                BracketNode(E, node_position);</div><div class="line"></div><div class="line">            <span class="keyword">if</span> (E - &gt; Lists[node_position - <span class="number">1</span>] - &gt; c == <span class="string">'+'</span> || E - &gt; Lists[node_position - <span class="number">1</span>] - &gt; c == <span class="string">'-'</span>)</div><div class="line">                BracketNode(E, node_position - <span class="number">1</span>);</div><div class="line"></div><div class="line">            ConnectNode(E, node_position, str[i]); <span class="comment">//连接两个链表</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    PrintNode(E - &gt; Lists[<span class="number">0</span>]); <span class="comment">//输入最后的表达式</span></div><div class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>
    
    <div>
      
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script type="text/javascript" src="http://jslibs.wuxubj.cn/sweetalert_mini/jquery-1.7.1.min.js"></script>
  <script src="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.mini.css">

  <p><span>本文标题:</span><a href="/posts/c02ba867/">后缀表达式转中缀表达式</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Vitah 的个人博客">Vitah</a></p>
  <p><span>发布时间:</span>2016年12月28日 - 23:12</p>
  <p><span>最后更新:</span>2016年12月28日 - 23:12</p>
  <p><span>原始链接:</span><a href="/posts/c02ba867/" title="后缀表达式转中缀表达式">http://www.vitah.net/posts/c02ba867/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://www.vitah.net/posts/c02ba867/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>

<script> 
    var clipboard = new Clipboard('.fa-clipboard');
	clipboard.on('success', $(function(){
	  $(".fa-clipboard").click(function(){
		swal({   
		  title: "",   
		  text: '复制成功',   
		  html: false,
		  timer: 500,   
		  showConfirmButton: false
	    });
	  });
    }));  
</script>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div></div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/wechatpay.jpeg" alt="Vitah WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="/images/alipay.jpeg" alt="Vitah Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Algorithm/" rel="tag"># Algorithm</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/608bbd7b/" rel="next" title="npm使用技巧">
                <i class="fa fa-chevron-left"></i> npm使用技巧
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/48bce0b9/" rel="prev" title="Linux相关命令记录">
                Linux相关命令记录 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/head.JPG"
               alt="Vitah" />
          <p class="site-author-name" itemprop="name">Vitah</p>
           
              <p class="site-description motion-element" itemprop="description">深自缄默，如云漂泊。</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">47</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">14</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/vitahlin" target="_blank" title="Github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Github
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="mailto:linw1225@163.com" target="_blank" title="Mail">
                  
                    <i class="fa fa-fw fa-envelope"></i>
                  
                  Mail
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-block">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="https://shuge.org/" title="书格" target="_blank">书格</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://taozj.org/" title="taozj" target="_blank">taozj</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://oserror.com/" title="Charles" target="_blank">Charles</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.hsmouc.com/" title="Hsmouc" target="_blank">Hsmouc</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#题目来源"><span class="nav-number">1.</span> <span class="nav-text">题目来源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#问题描述"><span class="nav-number">2.</span> <span class="nav-text">问题描述</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#输入形式"><span class="nav-number">2.1.</span> <span class="nav-text">输入形式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#输出形式"><span class="nav-number">2.2.</span> <span class="nav-text">输出形式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代码实现"><span class="nav-number">3.</span> <span class="nav-text">代码实现</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Vitah</span>
</div>



        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      访客
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>


        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  








  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  

    
      <script id="dsq-count-scr" src="https://http-www-vitah-net.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://www.vitah.net/posts/c02ba867/';
          this.page.identifier = 'posts/c02ba867/';
          this.page.title = '后缀表达式转中缀表达式';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://http-www-vitah-net.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>





  

  

  

  

  

</body>
</html>
